package com.example.examsystem.security;

import com.example.examsystem.model.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义用户详情类
 */
public class CustomUserDetails implements UserDetails {
    
    private final User user;
    private final List<String> authorities;
    
    public CustomUserDetails(User user, List<String> authorities) {
        this.user = user;
        this.authorities = authorities;
    }
    
    public Long getId() {
        return user.getId();
    }
    
    public String getFullName() {
        return user.getFullName();
    }
    
    public String getEmail() {
        return user.getEmail();
    }
    
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }
    
    @Override
    public String getPassword() {
        return user.getPassword();
    }
    
    @Override
    public String getUsername() {
        return user.getUsername();
    }
    
    @Override
    public boolean isAccountNonExpired() {
        return user.getEnabled();
    }
    
    @Override
    public boolean isAccountNonLocked() {
        return !user.getAccountLocked();
    }
    
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    
    @Override
    public boolean isEnabled() {
        return user.getEnabled();
    }
    
    public User getUser() {
        return user;
    }
}
